package com.takeeasy.project.baiqin.action.org;

import java.util.ArrayList;
import java.util.List;

import com.takeeasy.platform.action.BaseAction;
import com.takeeasy.platform.annotation.Action;
import com.takeeasy.platform.dataobject.RequestDataObject;
import com.takeeasy.platform.dataobject.ResponseDataObject;
import com.takeeasy.platform.util.DBUtil;
import com.takeeasy.project.baiqin.bo.org.UserBo;

@Action(name = "UserAction")
public class UserAction extends BaseAction {
	public ResponseDataObject getAllUser(RequestDataObject req) {
		String sql = "select * from t_org_user";
		List<Object> params = new ArrayList<>();
		sql = buildParams(sql, req, params);
		List<UserBo> userList = getDao().query(UserBo.class, sql.toString(), params);
		getResponseData().addToResponseMap("users", userList);
		return getResponseData();
	}

	public ResponseDataObject gotoUserListPage(RequestDataObject req){
		getResponseData().setRedirectPage("admin/jsp/userList.jsp");
		return getResponseData();
	}
	
	private String buildParams(String sql, RequestDataObject req, List<Object> params) {
		String userId = req.getParams().get("userId");
		String userName = req.getParams().get("userName");
		String userNickName = req.getParams().get("userNickName");
		String userPassword = req.getParams().get("userPassword");
		String userGroup = req.getParams().get("userGroup");
		String userPromise = req.getParams().get("userPromise");
		if (userId != null) {
			sql = DBUtil.addSQLParameter(sql, "user_id", "and");
			params.add(userId);
		}
		if (userName != null) {
			sql = DBUtil.addSQLParameter(sql, "user_name", "and");
			params.add(userName);
		}
		if (userNickName != null) {
			sql = DBUtil.addSQLParameter(sql, "user_nick_name", "and");
			params.add(userNickName);
		}
		if (userPassword != null) {
			sql = DBUtil.addSQLParameter(sql, "user_password", "and");
			params.add(userPassword);
		}
		if (userGroup != null) {
			sql = DBUtil.addSQLParameter(sql, "user_group", "and");
			params.add(userGroup);
		}
		if (userPromise != null) {
			sql = DBUtil.addSQLParameter(sql, "user_promise", "and");
			params.add(userPromise);
		}
		return sql;
	}
}
